package com.bw.house.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.bw.house.dao.HouseNewroomDao;
import com.bw.house.entity.HouseNewroomEntity;
import com.bw.house.service.HouseNewroomService;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;


@Service("houseNewroomService")
public class HouseNewroomServiceImpl extends ServiceImpl<HouseNewroomDao, HouseNewroomEntity> implements HouseNewroomService {
@Autowired
private HouseNewroomDao houseNewroomDao;


    @Override
    public List<HouseNewroomEntity> list(Wrapper<HouseNewroomEntity> queryWrapper) {
        return houseNewroomDao.selectDesc();
    }

    @SuppressWarnings("all")
    @Autowired
    private RestHighLevelClient client;



    @Override
    public PageUtils queryPage(Map<String, Object> params) throws IOException {
        SearchRequest searchRequest = new SearchRequest("housenewroom");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //构建查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //获取前台传递给后台的房屋类型
        String roomHtype = (String) params.get("roomHtype");
        if (StringUtils.isNotEmpty(roomHtype)){//如果符合条件说明我们有一个精准查询
            boolQueryBuilder.must(QueryBuilders.termQuery("roomHtype",roomHtype));
        }

        Date date = new Date();
        long time = date.getTime();
        //判断是否开盘
        if (params.get("kp").equals(1+"")){
           //开盘  当前时间大于开盘时间
            boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").lte(time));
        }
        else{
            //未开盘 当前时间小于开盘时间
            boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").gt(time));
        }

        //价格区间
        Double start = (Double) params.get("start");
        Double end = (Double) params.get("end");
        if (start!=null && start!=0){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("roomPrice").gte(start)); //大于
        }
        if (end!=null && end!=0){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("roomPrice").lte(end)); //小于
        }
        //分页
        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        searchSourceBuilder.from(page);
        searchSourceBuilder.size((page-1)*limit);

        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        //同步执行
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();

        ArrayList<HouseNewroomEntity> list = new ArrayList<>();
        long numHits = totalHits.value;
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit hit : searchHits) {
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            HouseNewroomEntity houseNewroomEntity = JSON.parseObject(sourceAsString, new TypeReference<HouseNewroomEntity>() {
            });
            houseNewroomEntity.setRoomId(Long.parseLong(id));
            list.add(houseNewroomEntity);
        }
//List<?> list, int totalCount, int pageSize, int currPage
        return new PageUtils(list, (int) numHits,page,limit);
    }

}